home *** CD-ROM | disk | FTP | other *** search
/ Aminet 41 / Aminet 41 (2001)(Schatztruhe)[!][Feb 2001].iso / Aminet / dev / c / libiconv_src.lha / src / utf7.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-11-07  |  9.1 KB  |  332 lines

  1. /*
  2.  * UTF-7
  3.  */
  4.  
  5. /* Specification: RFC 2152 (and old RFC 1641, RFC 1642) */
  6. /* The original Base64 encoding is defined in RFC 2045. */
  7.  
  8. /* Set of direct characters:
  9.  *   A-Z a-z 0-9 ' ( ) , - . / : ? space tab lf cr
  10.  */
  11. static const unsigned char direct_tab[128/8] = {
  12.   0x00, 0x26, 0x00, 0x00, 0x81, 0xf3, 0xff, 0x87,
  13.   0xfe, 0xff, 0xff, 0x07, 0xfe, 0xff, 0xff, 0x07,
  14. };
  15. #define isdirect(ch) ((ch) < 128 && ((direct_tab[(ch)>>3] >> (ch & 7)) & 1))
  16.  
  17. /* Set of direct and optional direct characters:
  18.  *   A-Z a-z 0-9 ' ( ) , - . / : ? space tab lf cr
  19.  *   ! " # $ % & * ; < = > @ [ ] ^ _ ` { | }
  20.  */
  21. static const unsigned char xdirect_tab[128/8] = {
  22.   0x00, 0x26, 0x00, 0x00, 0xff, 0xf7, 0xff, 0xff,
  23.   0xff, 0xff, 0xff, 0xef, 0xff, 0xff, 0xff, 0x3f,
  24. };
  25. #define isxdirect(ch) ((ch) < 128 && ((xdirect_tab[(ch)>>3] >> (ch & 7)) & 1))
  26.  
  27. /* Set of base64 characters, extended:
  28.  *   A-Z a-z 0-9 + / -
  29.  */
  30. static const unsigned char xbase64_tab[128/8] = {
  31.   0x00, 0x00, 0x00, 0x00, 0x00, 0xa8, 0xff, 0x03,
  32.   0xfe, 0xff, 0xff, 0x07, 0xfe, 0xff, 0xff, 0x07,
  33. };
  34. #define isxbase64(ch) ((ch) < 128 && ((xbase64_tab[(ch)>>3] >> (ch & 7)) & 1))
  35.  
  36. /*
  37.  * The state is structured as follows:
  38.  * bit 1..0: shift
  39.  * bit 7..2: data
  40.  * Precise meaning:
  41.  *   shift      data
  42.  *     0         0           not inside base64 encoding
  43.  *     1         0           inside base64, no pending bits
  44.  *     2      XXXX00         inside base64, 4 bits remain from 2nd byte
  45.  *     3      XX0000         inside base64, 2 bits remain from 3rd byte
  46.  */
  47.  
  48. static int
  49. utf7_mbtowc (conv_t conv, wchar_t *pwc, const unsigned char *s, int n)
  50. {
  51.   state_t state = conv->istate;
  52.   int count = 0; /* number of input bytes already read */
  53.   if (state & 3)
  54.     goto active;
  55.   else
  56.     goto inactive;
  57.  
  58. inactive:
  59.   {
  60.     /* Here (state & 3) == 0 */
  61.     if (n < count+1)
  62.       goto none;
  63.     {
  64.       unsigned char c = *s;
  65.       if (isxdirect(c)) {
  66.         *pwc = (wchar_t) c;
  67.         conv->istate = state;
  68.         return count+1;
  69.       }
  70.       if (c == '+') {
  71.         if (n < count+2)
  72.           goto none;
  73.         if (s[1] == '-') {
  74.           *pwc = (wchar_t) '+';
  75.           conv->istate = state;
  76.           return count+2;
  77.         }
  78.         s++; count++;
  79.         state = 1;
  80.         goto active;
  81.       }
  82.       return RET_ILSEQ;
  83.     }
  84.   }
  85.  
  86. active:
  87.   {
  88.     /* base64 encoding active */
  89.     unsigned int wc = 0;
  90.     state_t base64state = state;
  91.     unsigned int kmax = 2; /* number of payload bytes to read */
  92.     unsigned int k = 0; /* number of payload bytes already read */
  93.     unsigned int base64count = 0; /* number of base64 bytes already read */
  94.     for (;;) {
  95.       unsigned char c = *s;
  96.       unsigned int i;
  97.       if (c >= 'A' && c <= 'Z')
  98.         i = c-'A';
  99.       else if (c >= 'a' && c <= 'z')
  100.         i = c-'a'+26;
  101.       else if (c >= '0' && c <= '9')
  102.         i = c-'0'+52;
  103.       else if (c == '+')
  104.         i = 62;
  105.       else if (c == '/')
  106.         i = 63;
  107.       else {
  108.         /* c terminates base64 encoding */
  109.         if (base64state & -4)
  110.           return RET_ILSEQ; /* data must be 0, otherwise illegal */
  111.         if (base64count)
  112.           return RET_ILSEQ; /* partial UTF-16 characters are invalid */
  113.         if (c == '-') {
  114.           s++; count++;
  115.         }
  116.         state = 0;
  117.         goto inactive;
  118.       }
  119.       s++; base64count++;
  120.       /* read 6 bits: 0 <= i < 64 */
  121.       switch (base64state & 3) {
  122.         case 1: /* inside base64, no pending bits */
  123.           base64state = (i << 2) | 0; break;
  124.         case 0: /* inside base64, 6 bits remain from 1st byte */
  125.           wc = (wc << 8) | (base64state & -4) | (i >> 4); k++;
  126.           base64state = ((i & 15) << 4) | 2; break;
  127.         case 2: /* inside base64, 4 bits remain from 2nd byte */
  128.           wc = (wc << 8) | (base64state & -4) | (i >> 2); k++;
  129.           base64state = ((i & 3) << 6) | 3; break;
  130.         case 3: /* inside base64, 2 bits remain from 3rd byte */
  131.           wc = (wc << 8) | (base64state & -4) | i; k++;
  132.           base64state = 1; break;
  133.       }
  134.       if (k == kmax) {
  135.         /* UTF-16: When we see a High Surrogate, we must also decode
  136.            the following Low Surrogate. */
  137.         if (kmax == 2 && (wc >= 0xd800 && wc < 0xdc00))
  138.           kmax = 4;
  139.         else
  140.           break;
  141.       }
  142.       if (n < count+base64count+1)
  143.         goto none;
  144.     }
  145.     /* Here k = kmax > 0, hence base64count > 0. */
  146.     if ((base64state & 3) == 0) abort();
  147.     if (kmax == 4) {
  148.       wchar_t wc1 = wc >> 16;
  149.       wchar_t wc2 = wc & 0xffff;
  150.       if (!(wc1 >= 0xd800 && wc1 < 0xdc00)) abort();
  151.       if (!(wc2 >= 0xdc00 && wc2 < 0xe000)) return RET_ILSEQ;
  152.       *pwc = 0x10000 + ((wc1 - 0xd800) << 10) + (wc2 - 0xdc00);
  153.     } else {
  154.       *pwc = wc;
  155.     }
  156.     conv->istate = base64state;
  157.     return count+base64count;
  158.   }
  159.  
  160. none:
  161.   conv->istate = state;
  162.   return RET_TOOFEW(count);
  163. }
  164.  
  165. /*
  166.  * The state is structured as follows:
  167.  * bit 1..0: shift
  168.  * bit 7..2: data
  169.  * Precise meaning:
  170.  *   shift      data
  171.  *     0         0           not inside base64 encoding
  172.  *     1         0           inside base64, no pending bits
  173.  *     2       XX00          inside base64, 2 bits known for 2nd byte
  174.  *     3       XXXX          inside base64, 4 bits known for 3rd byte
  175.  */
  176.  
  177. /* Define this to 1 if you want the so-called "optional direct" characters
  178.       ! " # $ % & * ; < = > @ [ ] ^ _ ` { | }
  179.    to be encoded. Define to 0 if you want them to be passed straight through,
  180.    like the so-called "direct" characters.
  181.    We set this to 1 because it's safer.
  182.  */
  183. #define UTF7_ENCODE_OPTIONAL_CHARS 1
  184.  
  185. static int
  186. utf7_wctomb (conv_t conv, unsigned char *r, wchar_t iwc, int n)
  187. {
  188.   state_t state = conv->ostate;
  189.   unsigned int wc = iwc;
  190.   int count = 0;
  191.   if (state & 3)
  192.     goto active;
  193.  
  194. /*inactive:*/
  195.   {
  196.     if (UTF7_ENCODE_OPTIONAL_CHARS ? isdirect(wc) : isxdirect(wc)) {
  197.       r[0] = (unsigned char) wc;
  198.       /*conv->ostate = state;*/
  199.       return 1;
  200.     } else {
  201.       *r++ = '+';
  202.       if (wc == '+') {
  203.         if (n < 2)
  204.           return RET_TOOSMALL;
  205.         *r = '-';
  206.         /*conv->ostate = state;*/
  207.         return 2;
  208.       }
  209.       count = 1;
  210.       state = 1;
  211.       goto active;
  212.     }
  213.   }
  214.  
  215. active:
  216.   {
  217.     /* base64 encoding active */
  218.     if (UTF7_ENCODE_OPTIONAL_CHARS ? isdirect(wc) : isxdirect(wc)) {
  219.       /* deactivate base64 encoding */
  220.       count += ((state & 3) >= 2 ? 1 : 0) + (isxbase64(wc) ? 1 : 0) + 1;
  221.       if (n < count)
  222.         return RET_TOOSMALL;
  223.       if ((state & 3) >= 2) {
  224.         unsigned int i = state & -4;
  225.         unsigned char c;
  226.         if (i < 26)
  227.           c = i+'A';
  228.         else if (i < 52)
  229.           c = i-26+'a';
  230.         else if (i < 62)
  231.           c = i-52+'0';
  232.         else if (i == 62)
  233.           c = '+';
  234.         else if (i == 63)
  235.           c = '/';
  236.         else
  237.           abort();
  238.         *r++ = c;
  239.       }
  240.       if (isxbase64(wc))
  241.         *r++ = '-';
  242.       state = 0;
  243.       *r++ = (unsigned char) wc;
  244.       conv->ostate = state;
  245.       return count;
  246.     } else {
  247.       unsigned int k; /* number of payload bytes to write */
  248.       if (wc < 0x10000) {
  249.         k = 2;
  250.         count += ((state & 3) >= 2 ? 3 : 2);
  251.       } else if (wc < 0x110000) {
  252.         unsigned int wc1 = 0xd800 + ((wc - 0x10000) >> 10);
  253.         unsigned int wc2 = 0xdc00 + ((wc - 0x10000) & 0x3ff);
  254.         wc = (wc1 << 16) | wc2;
  255.         k = 4;
  256.         count += ((state & 3) >= 3 ? 6 : 5);
  257.       } else
  258.         return RET_ILSEQ;
  259.       if (n < count)
  260.         return RET_TOOSMALL;
  261.       for (;;) {
  262.         unsigned int i;
  263.         unsigned char c;
  264.         switch (state & 3) {
  265.           case 0: /* inside base64, 6 bits known for 4th byte */
  266.             c = (state & -4) >> 2; state = 1; break;
  267.           case 1: /* inside base64, no pending bits */
  268.             i = (wc >> (8 * --k)) & 0xff;
  269.             c = i >> 2; state = ((i & 3) << 4) | 2; break;
  270.           case 2: /* inside base64, 2 bits known for 2nd byte */
  271.             i = (wc >> (8 * --k)) & 0xff;
  272.             c = (state & -4) | (i >> 4); state = ((i & 15) << 2) | 3; break;
  273.           case 3: /* inside base64, 4 bits known for 3rd byte */
  274.             i = (wc >> (8 * --k)) & 0xff;
  275.             c = (state & -4) | (i >> 6); state = ((i & 63) << 2) | 0; break;
  276.           default: abort(); /* stupid gcc */
  277.         }
  278.         if (c < 26)
  279.           c = c+'A';
  280.         else if (c < 52)
  281.           c = c-26+'a';
  282.         else if (c < 62)
  283.           c = c-52+'0';
  284.         else if (c == 62)
  285.           c = '+';
  286.         else if (c == 63)
  287.           c = '/';
  288.         else
  289.           abort();
  290.         *r++ = c;
  291.         if ((state & 3) && (k == 0))
  292.           break;
  293.       }
  294.       conv->ostate = state;
  295.       return count;
  296.     }
  297.   }
  298. }
  299.  
  300. static int
  301. utf7_reset (conv_t conv, unsigned char *r, int n)
  302. {
  303.   state_t state = conv->ostate;
  304.   if (state & 3) {
  305.     /* deactivate base64 encoding */
  306.     unsigned int count = ((state & 3) >= 2 ? 1 : 0) + 1;
  307.     if (n < count)
  308.       return RET_TOOSMALL;
  309.     if ((state & 3) >= 2) {
  310.       unsigned int i = state & -4;
  311.       unsigned char c;
  312.       if (i < 26)
  313.         c = i+'A';
  314.       else if (i < 52)
  315.         c = i-26+'a';
  316.       else if (i < 62)
  317.         c = i-52+'0';
  318.       else if (i == 62)
  319.         c = '+';
  320.       else if (i == 63)
  321.         c = '/';
  322.       else
  323.         abort();
  324.       *r++ = c;
  325.     }
  326.     *r++ = '-';
  327.     /* conv->ostate = 0; will be done by the caller */
  328.     return count;
  329.   } else
  330.     return 0;
  331. }
  332.